package com.gaussian.property.app.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gaussian.property.app.vo.Reply;
import com.gaussian.property.app.vo.message.ProjectMessageVo;
import com.gaussian.property.app.vo.message.ReplyForm;
import com.gaussian.property.module.biz.entity.AppUser;
import com.gaussian.property.module.biz.entity.ProjectMessage;
import com.gaussian.property.module.biz.entity.ProjectMessageReply;
import com.gaussian.property.module.biz.mapper.ProjectMessageMapper;
import com.gaussian.property.module.biz.service.IProjectMessageReplyService;
import com.gaussian.property.module.biz.vo.message.ProjectMessageDto;
import com.songqi.common.core.constant.Constants;
import com.songqi.common.core.exception.CustomException;
import com.songqi.common.core.utils.BeanCopyUtil;
import com.songqi.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

/**
 * @description:
 * @author: songqi(13776346982)
 * @date: 2024/4/19 14:57
 */

@Service
public class MessageService {


    @Resource
    private ProjectMessageMapper projectMessageMapper;

    @Resource
    private IProjectMessageReplyService iProjectMessageReplyService;


    /**
     *
     * @param month
     * @param projectId
     * @return
     */
    public List<ProjectMessageVo> queryMessage(String month, String projectId, String appUserId) {
        // 创建一个DateTimeFormatter来解析包含中文字符的日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        // 解析日期字符串
        LocalDate date = LocalDate.parse(month + "01", formatter);
        // 获取月份的最后一天
        LocalDate lastDayOfMonth = date.with(TemporalAdjusters.lastDayOfMonth());

        LocalDateTime startTime = LocalDateTime.of(date, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(lastDayOfMonth, LocalTime.MAX);
        List<ProjectMessageDto> projectMessageDtoList = projectMessageMapper.queryProjectMessage(startTime, endTime, projectId);
        List<ProjectMessageVo> projectMessageVoList = BeanCopyUtil.copyList(projectMessageDtoList, ProjectMessageVo.class);
        if(CollectionUtil.isNotEmpty(projectMessageVoList)){
            for (ProjectMessageVo projectMessageVo : projectMessageVoList) {
                List<ProjectMessageReply> messageReplies = queryMessageReplies(projectMessageVo.getProjectMessageId(), appUserId);
                List<Reply> replyList = BeanCopyUtil.copyList(messageReplies, Reply.class);
                projectMessageVo.setReplyList(replyList);
            }
        }

        return projectMessageVoList;
    }


    /**
     * 回复留言
     * @param replyForm
     */
    @Transactional(rollbackFor = Exception.class)
    public void reply(ReplyForm replyForm) {
        AppUser appUser = (AppUser) SecurityUtils.getTokenUser();
        String projectMessageId = replyForm.getProjectMessageId();
        ProjectMessage projectMessage = projectMessageMapper.selectById(projectMessageId);
        if(projectMessage == null){
            throw new CustomException("【参数错误】项目留言Id不存在");
        }
        if(projectMessage.getIsReply() == Constants.NO){
            projectMessage.setIsReply(Constants.YES);
            projectMessageMapper.updateById(projectMessage);
        }
        ProjectMessageReply projectMessageReply = new ProjectMessageReply();
        projectMessageReply.setProjectMessageId(projectMessageId);
        projectMessageReply.setContent(replyForm.getContent());
        projectMessageReply.setReplierRealName(appUser.getRealName());
        projectMessageReply.setReplySource(Constants.APP_SOURCE);
        iProjectMessageReplyService.save(projectMessageReply);
    }

    private List<ProjectMessageReply> queryMessageReplies(String projectMessageId, String appUserId) {
        LambdaQueryWrapper<ProjectMessageReply> queryWrapper = new LambdaQueryWrapper<ProjectMessageReply>()
                .eq(ProjectMessageReply::getProjectMessageId, projectMessageId)
                .eq(ProjectMessageReply::getCreateBy, appUserId)
                .orderByDesc(ProjectMessageReply::getCreateTime);
        return iProjectMessageReplyService.list(queryWrapper);

    }


}
